package video.com.service.impl;

import com.Exception.GlobalException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.constant.ResponseDataConstant;
import com.responseData.ResponseData;
import com.utils.GenerateNum;
import jodd.util.StringUtil;
import video.com.entity.Tag;
import video.com.mapper.TagMapper;
import video.com.service.TagService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.awt.*;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xwj
 * @since 2024-03-05
 */
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {

    @Resource
    private TagMapper tagMapper;

    @Override
    public Tag tagInfo(String tagContent) throws Exception {
        if(tagContent == null){
            throw new GlobalException(ResponseData.error("参数错误"));
        }

        LambdaQueryWrapper<Tag> tagLambdaQueryWrapper = new LambdaQueryWrapper<Tag>();
        tagLambdaQueryWrapper.eq(StringUtil.isNotEmpty(tagContent),Tag::getContent,tagContent);

        Tag tag = this.getOne(tagLambdaQueryWrapper,false);

        if(tag == null){

            tag = new Tag();
            tag.setTagId(GenerateNum.generateOrder());
            tag.setContent(tagContent);
            this.save(tag);

        }

        return tag;
    }

    @Override
    public List<Tag> getTagListByVid(String vid) throws GlobalException {

        if(vid == null || "".equals(vid)){
            throw new GlobalException(ResponseData.error("传入数据为空"));
        }

        return tagMapper.getTagListByVid(vid);
    }

    @Override
    public void batchInsertIntoVideoTag(List<String> tagIds , String video){
        tagMapper.batchInsetIntoVideoTag(tagIds,video);
    }
}
